package com.m3958.firstgwt.client.window;

import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.m3958.firstgwt.client.AppStatus;
import com.m3958.firstgwt.client.IhasSiteSelector;
import com.m3958.firstgwt.client.datasource.WebSiteDataSource;
import com.m3958.firstgwt.client.types.SmartSubOperationName;
import com.m3958.firstgwt.client.types.WebSiteField;
import com.m3958.firstgwt.shared.type.CommonField;
import com.m3958.firstgwt.shared.type.SmartParameterName;
import com.m3958.gwtutil.clientside.client.LoginOutService;
import com.m3958.gwtutil.clientside.client.jso.WebSiteJso;
import com.smartgwt.client.data.Criteria;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.SelectionStyle;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.events.SelectionChangedHandler;
import com.smartgwt.client.widgets.grid.events.SelectionEvent;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;

@Singleton
public class SiteSelectWindow extends Window{


	private ListGrid siteGrid;
	
	private boolean initialized = false;
	
	@Inject
	private AppStatus aservice;
	
	@Inject
	private LoginOutService lios;
	
	@Inject
	private WebSiteDataSource wsds;
	
	private IhasSiteSelector requester;
	
	public SiteSelectWindow(){
		setWidth(600);
		setHeight(450);
		setIsModal(true);
		setShowModalMask(true);
		setTitle("站点选择");
		centerInPage();
	}
	
	public SiteSelectWindow showMe(IhasSiteSelector requester){
		this.requester = requester;
		if(!initialized){
			initialized = true;
			VLayout layout = new VLayout(5);
			layout.addMember(initGrid());
			layout.addMember(initBt());
			addItem(layout);
			fetchGridData();
		}
		show();
		return this;
	}
	

	private void fetchGridData() {
	    Criteria c = new Criteria();
	    if(!lios.getLoginedUser().isSuperman()){
	    	c.addCriteria(SmartParameterName.SUB_OPERATION_TYPE, SmartSubOperationName.HAS_PERMISSION.toString());
	    }
	    c.addCriteria("_jsonv", "vv");
		c.addCriteria(CommonField.TIMEZONE.getValue(),aservice.getTimeZone());
		siteGrid.filterData(c);
	}

	private HLayout initBt() {
		HLayout hl = new HLayout();
		hl.setHeight(25);
		hl.setAlign(Alignment.RIGHT);
		IButton ib = new IButton("关闭");
		ib.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				SiteSelectWindow.this.hide();
			}
		});
		hl.addMember(ib);
		return hl;
	}

    
	private ListGrid initGrid() {
		siteGrid = new ListGrid();
		siteGrid.setDataSource(wsds);
		siteGrid.setAutoFetchData(false);
		siteGrid.setSelectionType(SelectionStyle.SINGLE);
		ListGridField idField = new ListGridField("id");
		ListGridField cnameField = new ListGridField(WebSiteField.CNAME.getValue(),"名称");
		siteGrid.setShowEmptyMessage(true);
		siteGrid.setEmptyMessage("诗篇建站系统中的许多对象具有website属性，即使你使用诗篇建站系统提供的通用工具用于外部站点，也需要在这里建立一个站点。请先尝试建立一个站点。<a href=\"#website_edit\">新建站点</a>");
		siteGrid.addSelectionChangedHandler(new SelectionChangedHandler() {
			@Override
			public void onSelectionChanged(SelectionEvent event) {
				Record r = event.getRecord();
				if(r!=null){
					requester.onSiteSelected((WebSiteJso) r.getJsObj());
					SiteSelectWindow.this.hide();
				}
			}
		});
		siteGrid.setFields(cnameField,idField);
		return siteGrid;
	}
}
